﻿using System.Text;
using AventStack.ExtentReports;
using Ro.AssistFunction.ExtentReport;
using Ro.Common.RoInterface;
using Ro.Common.RoInterface.UserType;

namespace Ro.AssistFunction.OutputManage
{
    /// <summary>
    /// 写入GUI的对外输出类
    /// </summary>
    public class RoOutputToGui
    {
        private readonly GuiOutEvent _guiOutEvent = new GuiOutEvent();

        /// <summary>
        /// 输出到Result框一栏
        /// </summary>
        /// <param name="name">用例名</param>
        /// <param name="stepname">步骤名</param>
        /// <param name="controlid">控件名</param>
        /// <param name="result">结果</param>
        /// <param name="extrainfo">追加信息</param>
        public void OutResult(string name, string stepname, string controlid, string result, string extrainfo)
        {
            ProOutToGui e = new ProOutToGui
            {
                Name = name,
                ControlId = controlid,
                StepsName = stepname,
                Result = result,
                ExtraInfo = extrainfo
            };
            //ER输出
            RebackExtentReport(result.ToLower() == "pass" ? Status.Pass : Status.Fail, stepname, controlid, extrainfo);
            //Result输出
            _guiOutEvent.OnChangeResult(e);
        }

        /// <summary>
        /// 输出到Detail一栏
        /// </summary>
        /// <param name="timeflag">时间戳</param>
        /// <param name="details">详情</param>
        public void OutDetail(string timeflag, string details)
        {
            ProOutToGui e = new ProOutToGui
            {
                TimeFlag = timeflag,
                Details = details
            };
            _guiOutEvent.OnChangeDetail(e);
        }


        /// <summary>
        /// 返回extentreport
        /// 带有附加信息
        /// </summary>
        /// <param name="roLogStatus">状态</param>
        /// <param name="stepname">操作名称</param>
        /// <param name="controlid">控件ID</param>
        /// <param name="extrainfo">追加信息</param>
        private void RebackExtentReport(Status roLogStatus, string stepname, string controlid, string extrainfo)
        {
            //初始化
            IRoExtentReport roExtentReport = new RoExtentReport();

            stepname = "操作名称:<br/>" + stepname;

            StringBuilder exdetails = new StringBuilder();
            exdetails.AppendLine("操作的控件ID:" + controlid + "<br/>");
            exdetails.AppendLine("额外追加信息:<br/>" + extrainfo + "<br/>");
            roExtentReport.AddTestsChildNode(roLogStatus, stepname, exdetails.ToString());
        }
    }


    /// <summary>
    /// 主要存放RoOutputToGui的事件
    /// </summary>
    public class GuiOutEvent
    {
        /// <summary>
        /// 定义委托，更新UI
        /// <para>更新ResultResult</para>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="item"></param>
        public delegate void InvokeReuslt(object sender, ProOutToGui item);

        /// <summary>
        /// 定义委托，更新UI
        /// <para>更新ResultDetails</para>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="item"></param>
        public delegate void InvokeDetails(object sender, ProOutToGui item);


        /// <summary>
        /// 定义在GuiOut中的一个事件，参数是MessageArgs对象
        /// </summary>
        public static event InvokeReuslt ChangeResult;


        /// <summary>
        /// 定义在GuiOut中的一个事件，参数是MessageArgs对象
        /// </summary>
        public static event InvokeDetails ChangeDetail;


        /// <summary>
        /// 触发更改UI的事件
        /// 更改Result
        /// </summary>
        /// <param name="e"></param>
        public void OnChangeResult(ProOutToGui e)
        {
            ChangeResult?.Invoke(this, e);
        }


        /// <summary>
        /// 触发更改UI的事件
        /// 更改Detail
        /// </summary>
        /// <param name="e"></param>
        public void OnChangeDetail(ProOutToGui e)
        {
            ChangeDetail?.Invoke(this, e);
        }
    }
}
